package com.shiro.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

/**
 * Created with IntelliJ IDEA.
 * User: dengpeng
 * Date: 2018/7/5
 * Time: 13:30
 * Description: 加密解密
 */
public class DESUtils {

    /**
     * DES密钥
     */
    private static String key = "sdiukahfidhf$I%U()(I((#*()*^hksdhf";

    /**
     * DES加密
     *
     * @param data:需要加密的数据
     * @return
     */
    public static String encryptDES(String data) throws Exception {

        String s = null;
        if (data != null) {
            //DES算法要求有一个可信任的随机数源
            SecureRandom sr = new SecureRandom();
            //从原始密钥数据创建DESKeySpec对象
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            //创建一个密钥工厂，用它将DESKeySpec转化成SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKeySpec);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密钥初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
            //将加密后的数据编码成字符串
            BASE64Encoder encoder = new BASE64Encoder();
            s = encoder.encode(cipher.doFinal(data.getBytes()));
        }
        return s;
    }

    /**
     * DES解密
     *
     * @param data:需要解密的数据
     * @return
     * @throws Exception
     */
    public static String decryptDES(String data) throws Exception {

        String s = null;
        if (data != null) {
            //DES算法要求有一个可信任的随机数源
            SecureRandom sr = new SecureRandom();
            //从原始密钥数据创建DESKeySpec对象
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            //创建一个密钥工厂，用它将DESKeySpec转化成SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKeySpec);
            //Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密钥初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
            //将加密后的数据解码再解密
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] buf = cipher.doFinal(base64Decoder.decodeBuffer(data));
            s = new String(buf);
        }
        return s;
    }

    public static void main(String[] args) {

        String data = "123";
        //加密
        try {
            String str1 = encryptDES(data);
            System.out.println("加密后为:str1=" + str1);
            //解密
            String str2 = decryptDES(str1);
            System.out.println("解密后为:str2=" + str2);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
